// pages/profile/index.ts
const app = getApp()
// const pageStack = getCurrentPages()
// console.log(pageStack, 'pageStack')
Page({
  data: {
    avatar: '',
    showPopup: false,
    showGender: false,
    showBirthday: false,
    showAddress: false,
    showNotes: false
  },
  onShow() {
    this.getUserProfile()
  },
  async getUserProfile() {
    // 1. 发送请求获取用户信息
    const { errno, data: { userInfo } } = await wx.http.get('/user/getUserInfo')
    console.log(userInfo, '=====')
    // 2. 判断请求是否成功
    if (errno !== 0) return wx.utils.toast('获取用户信息失败！')
    // 3. 保存到data中
    this.setData({ ...userInfo })
    // 4. 渲染
    // 5. 将用户信息存储到全局的应用实例
    app.userProfiles = { id: userInfo.id }
  },

  async update(data) {
    const { errmsg, errno } = await wx.http.post('/user/update', {
      id: this.data.id,
      ...data
    })
    if (errno !== 0) {
      wx.utils.toast(errmsg + '请重试')
      return false
    }
    wx.utils.toast(errmsg)
    return true
  },
  // 修改用户名
  updateUsername(ev) {
    const username = ev.detail.value
    if (username === '') return wx.utils.toast('昵称不能为空')
    this.update({ username })
  },
  // 修改性别
  openGenderLayer() {
    this.setData({ showGender: true })
  },
  onGenderClose() {
    this.setData({ showGender: false })
  },
  onConfirmGender(ev) {
    const sex = ev.detail.value === '男' ? 1 : 0
    if (this.update({ sex })) {
      this.setData({
        sex,
        showGender: false
      })
    }
  },
  // 修改出生年月
  openBirthdayLayer() {
    this.setData({ showBirthday: true })
    this.setData({ current: new Date(this.data.birthday).getTime() })
  },
  onBirthdayClose() {
    this.setData({ showBirthday: false })
  },
  onConfirmBirthday(ev) {
    let { birthday } = this.data
    birthday = new Date(ev.detail).toLocaleDateString().replaceAll('/', '-')
    if (this.update({ birthday })) {
      this.setData({
        birthday,
        showBirthday: false
      })
    }
  },
  // 家庭住址
  openAddressLayer() {
    this.setData({ showAddress: true })
  },
  onAddressClose() {
    this.setData({ showAddress: false })
  },
  onConfirmAddress() {
    const { address } = this.data
    if (this.update({ address })) {
      this.setData({
        showAddress: false
      })
    }
  },
  // 个性留言
  openNotesLayer() {
    this.setData({ showNotes: true })
  },
  onNotesClose() {
    this.setData({ showNotes: false })
  },
  async onConfirmNotes() {
    const { notes } = this.data
    if (await this.update({ notes })) {
      this.setData({
        showNotes: false
      })
    }
  },










  getUserAvatar(ev) {
    // 本地上传成功之后返回可以访问的一个链接 ==> 替代文件对象
    console.log(ev.detail.avatarUrl)
    this.updateAvatar(ev.detail.avatarUrl)
  },
  async updateAvatar(avatar) {
    wx.uploadFile({
      filePath: avatar,
      name: 'file', // 后端接收文件名称
      url: wx.http.baseURL + '/common/upload?type=images', // 服务器地址
      formData: { type: 'avatar' },
      header: {
        token: getApp().token
      },
      success: res => {
        console.log(res)
        const { errno, data } = JSON.parse(res.data)
        console.log(data, 'datadatadata')
        if (errno !== 0) return wx.utils.toast('上传用户头像失败！')
        // // pageStack[0].setData({ avatar: data.data.url })
        this.setData({ avatar: 'http://124.223.14.236:8060/' + data.savePath })
        app.userProfiles.avatar = this.data.avatar
        this.update({ icon: data.savePath })
      }
    })
  }
})